﻿using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Galaxy;

namespace Test
{
	class Program
	{
		static ConversionEnergyProducer P;
		static void Main(string[] args)
		{
			Console.WriteLine("Energy system test:");
			P = new ConversionEnergyProducer(500);

			Console.WriteLine("Producer: 500E/s material converter");
			Console.WriteLine("Material: TestMaterial");
			Console.WriteLine("Starting amount: 1000kg");
			Console.WriteLine("Stored energy: 1000E/kg");
			Console.WriteLine("Burn rate: 1kg/Es");

			P.SetMaterial("TestMaterial", 100, 1000, 1);
			P.Configure(50, 1);
			P.RequestPower(500);
			var T = new Thread(DisplayThread);
			T.Start();
			ConsoleKey C = ConsoleKey.Q;
			while (C != ConsoleKey.Escape)
			{
				C = Console.ReadKey(true).Key;
				switch (C)
				{
					case ConsoleKey.DownArrow:
						P.OutPutSwitch = Math.Max(0, P.OutPutSwitch - 0.1);
						break;
					case ConsoleKey.UpArrow:
						P.OutPutSwitch = Math.Min(1, P.OutPutSwitch + 0.1);
						break;
					case ConsoleKey.PageDown:
						P.RequestedPower = Math.Max(0, P.RequestedPower - 100);
						break;
					case ConsoleKey.PageUp:
						P.RequestedPower = Math.Min(500, P.RequestedPower + 100);
						break;
				}
			}
			End = true;
		}
		static bool End = false;
		static int Time = 0;
		
		static void DisplayThread()
		{
			while (!End)
			{
				Time++;
				Thread.Sleep(100);
				for (int i = 0; i < 20; i++)
					P.Process(0.005);
				Console.SetCursorPosition(0, 8);
				Console.Write((Time / 10.0).ToString("0.0").PadLeft(10));
				Console.SetCursorPosition(0, 9);
				Console.WriteLine("Requested power: " + P.RequestedPower.ToString("0.0"));
				Console.WriteLine("Current output: " + P.CurrentEnergyStream.Value.ToString("0.0"));
				Console.WriteLine("Output lever: " + (P.OutPutSwitch * 100).ToString("000.0") + "%");
				Console.WriteLine("Requested level: " + (P.RequestedPower / P.MaxPower * 100).ToString("000.0") + "%");
				Console.WriteLine("\nMaterial amount: " + P.Amount.ToString("0.0").PadLeft(5) + "kg");
				Console.WriteLine("\nMaterial efficiency (50kg min): " + (P.MaterialEfficiency * 100).ToString("000.0") + "%");

				Console.MoveBufferArea(50, 10, 70, 11, 49, 10);
				Console.SetCursorPosition(119, 20 - (int)(P.MaterialEfficiency * 10));
				Console.Write("X");
			}
		}

		static void DrawLog(ConversionEnergyProducer P)
		{
			Console.WriteLine("Output: " + P.OutPutSwitch.ToString("0.00") + " Material: " + P.Amount.ToString("0000.00") + "kg");
		}
	}
}
